//
//  SecondaryPages.m
//  BuffetNews
//
//  Created by weidawei on 14-11-5.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SecondaryPages.h"
#import "NetHandler.h"
#import "ModelPages.h"
#import "myViewCell.h"
#import "UIImageView+WebCache.h"
#import "ImageCell.h"
#import "UIImage+Tint.h"
#import "UIColor+myColor.h"
#import "Reachability.h"
#import "SecondViewController.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import "SecondViewController.h"
@interface SecondaryPages ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UITableView * table;
@property (nonatomic, retain) NSMutableArray * arrMode;
@property (nonatomic, retain) NSString * bigImage;

@end

@implementation SecondaryPages

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.arrMode = [NSMutableArray array];
       
    }
    return self;
}
- (void)LoadTheData
{
    BOOL reachable = [[Reachability reachabilityForInternetConnection] isReachable];
    
    if (!reachable) {
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(5, self.view.frame.size.height / 13 * 12, self.view.frame.size.width / 7, self.view.frame.size.height / 14 );
        [button setBackgroundImage:[UIImage imageNamed:@"btn_home.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(block:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        
        UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"请检查您的网络连接状态" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] autorelease];
        [alertView show];
        
        
        
    } else {
    [NetHandler networkWithURL:self.aipString comaletion:^(id result) {
        
        NSDictionary * dic = result;
        NSDictionary * dic1 = [dic objectForKey:@"data"];
        
        NSDictionary * dic2 = [dic1 objectForKey:@"head"];
        self.bigImage = [dic2 objectForKey:@"img"];
        
                               
        NSLog(@"98798797*********%@", dic1);
        
        NSMutableArray * arr = [NSMutableArray array];
        arr = [dic1 objectForKey:@"list"];
        for (NSDictionary * dic2 in arr) {
            ModelPages * modetitle = [[ModelPages alloc] init];
            modetitle.title = [dic2 objectForKey:@"title"];
            modetitle.stitle = [dic2 objectForKey:@"stitle"];
            modetitle.img = [dic2 objectForKey:@"pic"];
            modetitle.color = [dic2 objectForKey:@"block_color"];
            modetitle.apiString = [dic2 objectForKey:@"api_url"];
            [self.arrMode addObject:modetitle];
            [self startTablView];
            [modetitle release];
        }
        
        
    }];
    }
}

- (void)block:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

-(BOOL)isReachableViaWWAN
{
    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}

// 视图
- (void)startTablView
{
    self.table = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [_table  release];
    
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    [self LoadTheData];
    
//    self.view.backgroundColor = [UIColor whiteColor];
    NSLog(@"**********%@", self.aipString);
    // Do any additional setup after loading the view.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrMode.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    myViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
    if (!cell) {
        cell = [[[myViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1"] autorelease];
        
    }
    if (indexPath.row == 0) {
        ImageCell * cell = [tableView dequeueReusableCellWithIdentifier:@"2"];
        if (!cell) {
            cell = [[[ImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"2"] autorelease];
        }
        [cell.iamgeBing sd_setImageWithURL:[NSURL URLWithString:self.bigImage]];
        return cell;
        
    } else {
    ModelPages * mode1 = self.arrMode[indexPath.row];
    cell.label1.text = mode1.title;
    cell.label2.text = mode1.stitle;
    [cell.imageCell sd_setImageWithURL:[NSURL URLWithString:mode1.img]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        cell.imageCell.image = [cell.imageCell.image imageWithTintColor:[UIColor colorWithHexString:mode1.color]];
        
    }];
    
    
    return cell;
    
    }
    return cell;
}

/// 问题来了
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    SecondViewController * second = [[SecondViewController alloc] init];
//    second.apiString = 
//    
//}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ModelPages * modetitle = [[ModelPages alloc] init];
    modetitle = self.arrMode[indexPath.row];
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    secondVC.apiString = modetitle.apiString;
    [secondVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self presentViewController:secondVC animated:YES completion:^{
        
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 90;
    } else {
        return 60;
        
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
